package com.zk.shop.servlet.goods;

import com.zk.shop.entity.Goods;
import com.zk.shop.entity.Sorts;
import com.zk.shop.entity.User;
import com.zk.shop.service.GoodsService;
import com.zk.shop.service.Impl.GoodsServicesImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @version 1.0
 * @ClassName AddGoodsServlet
 * @Description TODO
 * @Author 关轶阳
 * @Date 2019/9/29 0029 下午 6:58
 */
@WebServlet("/AddGoodsServlet")
public class AddGoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String goodsName = request.getParameter("goodsName");
        Double price = Double.valueOf(request.getParameter("price"));
        int goodsNum = Integer.parseInt(request.getParameter("goodsnum"));
        String image = request.getParameter("goodsimage");
        String goodsContent = request.getParameter("goodsContent");
        int sortsId = Integer.parseInt(request.getParameter("sortsId"));
        Sorts sorts = new Sorts(sortsId);
        //创建session对象
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");


        Goods goods = new Goods(goodsName, price, goodsNum, image, goodsContent, user, sorts);
        GoodsService goodsServices = new GoodsServicesImpl();
        int flag = goodsServices.AddGoods(goods);
        PrintWriter out = response.getWriter();
        if (flag > 0) {
            out.print("添加成功");
        } else {
            out.print("添加失败");
        }

    }
}
